package SysSettingService

import (
	toolsDb "gdshop-back-end-go/common/db"
	"gdshop-back-end-go/common/libs/myCache"
	"gdshop-back-end-go/common/request/BaseReq"
	"gdshop-back-end-go/common/response"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"time"
)

func Update(r *ghttp.Request, req *BaseReq.I) *response.JsonResponse {
	configKey := r.GetString("config_key")
	if configKey == "" {
		return response.FailByRequestMessage(r, "参数错误")
	}
	configValue := r.GetString("config_value")
	if configValue == "" {
		configValue = "{}"
	}
	update, err := toolsDb.GetUnSafaTableAddDeleteWhere(r.GetCtx(), req.TableName).Data(g.Map{
		"config_value": configValue,
		"config_key":   configKey,
		"config_name":  "团购设置",
		"update_at":    time.Now().Unix(),
	}).WherePri("config_key", configKey).Save()
	if err != nil {
		return response.FailByRequestMessage(r, "更新失败："+err.Error())
	}
	affected, err := update.RowsAffected()
	if err != nil {
		return response.FailByRequestMessage(r, "更新失败："+err.Error())
	}
	if affected == 0 {
		return response.FailByRequestMessage(r, "更新失败：找不到数据")
	}
	// 清除缓存
	myCache.ClaerCaches(CachePrefix + configKey)

	return response.SuccessByRequestMessage(r, "更新成功")
}
